package com.zhenwei.zscm.repo;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.zhenwei.zscm.common.base.MybatisWrapper;
import com.zhenwei.zscm.common.utils.Linq;
import com.zhenwei.zscm.entity.RoleAuthEntity;
import com.zhenwei.zscm.mapper.IRoleAuthMapper;
import com.zhenwei.zscm.mappings.RoleAuthMapping;
import com.zhenwei.zscm.model.dto.roleAuth.RoleAuthDTO;
import com.zhenwei.zscm.model.vo.menu.MenuVO;
import com.zhenwei.zscm.model.vo.roleAuth.RoleAuthApiVO;
import com.zhenwei.zscm.model.vo.roleAuth.RoleAuthVO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleAuthRepo extends ServiceImpl<IRoleAuthMapper, RoleAuthEntity> {

    public RoleAuthDTO getRoleAuth(String role) {
        var q = new LambdaQueryWrapper<RoleAuthEntity>()
                .eq(RoleAuthEntity::getRole, role)
                .eq(RoleAuthEntity::getDeleted, false);
        var list = this.list(q);
        return Linq.getFirst(list, RoleAuthMapping.INSTANCE::entity2Dto);
    }

    public boolean createRoleAuth(RoleAuthDTO roleAuth) {
        var e = RoleAuthMapping.INSTANCE.dto2Entity(roleAuth);
        e.setGuid(IdUtil.simpleUUID());
        return this.save(e);
    }
    public boolean createRoleAuth(RoleAuthVO roleAuth) {
        var e = RoleAuthMapping.INSTANCE.vo2Entity(roleAuth);
        e.setGuid(IdUtil.simpleUUID());
        return this.save(e);
    }
    public boolean updateRoleAuth(RoleAuthDTO roleAuth) {
        var u =new LambdaUpdateWrapper<RoleAuthEntity>()
                .eq(RoleAuthEntity::getRole, roleAuth.getRole())
                .eq(RoleAuthEntity::getDeleted, false)
                .set(RoleAuthEntity::getAuthMenu, roleAuth.getAuthMenu())
                .set(RoleAuthEntity::getAuthApi, roleAuth.getAuthApi());
        MybatisWrapper.fillUser(u);
        return this.update(u);
    }

    public boolean deleteRoleAuth(String role) {
        var u = new LambdaUpdateWrapper<RoleAuthEntity>()
                .eq(RoleAuthEntity::getRole, role)
                .eq(RoleAuthEntity::getDeleted, false)
                .set(RoleAuthEntity::getDeleted, true);
        return this.update(u);
    }

    public List<RoleAuthDTO> all(){
        var q = new LambdaQueryWrapper<RoleAuthEntity>()
                .eq(RoleAuthEntity::getDeleted, false)
                .orderByDesc(RoleAuthEntity::getId);
        var list = this.list(q);
        return Linq.mapList(list, RoleAuthMapping.INSTANCE::entity2Dto);
    }

    public boolean updateMenuAuth(String role,String roleName, List<MenuVO> menus) throws JsonProcessingException {
//        var entityList = Linq.mapList(menus, MenuMapping.INSTANCE::vo2Entity);
        var u = new LambdaUpdateWrapper<RoleAuthEntity>()
                .eq(RoleAuthEntity::getRole, role)
                .eq(RoleAuthEntity::getDeleted, false)
                .set(RoleAuthEntity::getRoleName, roleName)
                .set(RoleAuthEntity::getAuthMenu, JacksonTypeHandler.getObjectMapper().writeValueAsString(menus));
        MybatisWrapper.fillUser(u);
        return this.update(u);
    }

    /**
     * 添加一个没有任何权限的角色权限
     * @param code 角色编码
     * @param name 角色名称
     * @return bool
     */
    public boolean createEmptyAuth(String code, String name) {
        var e = new RoleAuthEntity();
        e.setGuid(IdUtil.simpleUUID());
        e.setRole(code);
        e.setRoleName(name);
        e.setAuthMenu(new ArrayList<>());
        e.setAuthApi(new ArrayList<>());
        return this.save(e);
    }

    public boolean updateApiAuth(String role,String roleName, List<RoleAuthApiVO> apis) throws JsonProcessingException {
        var u = new LambdaUpdateWrapper<RoleAuthEntity>()
                .eq(RoleAuthEntity::getRole, role)
                .eq(RoleAuthEntity::getDeleted, false)
                .set(RoleAuthEntity::getRoleName, roleName)
                .set(RoleAuthEntity::getAuthApi, JacksonTypeHandler.getObjectMapper().writeValueAsString(apis));
        MybatisWrapper.fillUser(u);
        return this.update(u);
    }
}
